package com.wude.guild;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.dialog.PaySuccessDialog;
import com.wude.guild.fragment.BindCardFragment;
import com.wude.guild.fragment.PayTelecomFragment;
import com.wude.guild.fragment.SharedFragment;
import com.wude.guild.servercore.mints.MintsBaseActivity;
import com.wude.http.HttpCom;
import com.wude.tools.Util;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 运营商 判断 界面分发
 */
public class OperatorManagerActivity extends MintsBaseActivity {
    private String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operator_manager);

        tag = getIntent().getStringExtra("tag");
        //判断是否登录
        if (Utils.getLoginUser() == null) {
            startActivityForResult(new Intent(this, LoginActivity.class), 11);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("token", Utils.getLoginUser().token);
            HttpCom.POST(handler, HttpCom.getGetUserStatus(), map, false);
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONObject data = jsonObject.getJSONObject("data");
                        jump(data.getString("is_new"), data.getString("is_pay"), data.getString("bindphone"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtils.showLongToast("网络异常！");
                    break;
            }
        }
    };

    private void jump(String is_new, String is_pay, String phone) {
        if ("0".equals(is_new) && "0".equals(is_pay)) {//不是新用户
            pushFragmentToBackStack(BindCardFragment.class, tag);
        } else if ("1".equals(is_new) && "0".equals(is_pay)) {//电信、联通充值活动
            Map<String, String> data = new HashMap<>();
            data.put("tag", tag);
            data.put("phone", phone);
            pushFragmentToBackStack(PayTelecomFragment.class, data);
        } else if ("1".equals(is_pay)) {//已经参与过活动  进入分享页面
            pushFragmentToBackStack(SharedFragment.class, tag);
        }

//        PaySuccessDialog dialog = new PaySuccessDialog(this, this, "17311305259");
//        dialog.show();
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected int getFragmentContainerId() {
        return R.id.container;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (11 == requestCode) {
            UserInfo loginUser = Utils.getLoginUser();
            if (loginUser != null) {
                Map<String, String> map = new HashMap<>();
                map.put("token", Utils.getLoginUser().token);
                HttpCom.POST(handler, HttpCom.getGetUserStatus(), map, false);
            } else {
                finish();
            }
        }
    }
}
